<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/1/18
 * Time: 9:58
 */

namespace app\controller\index;
use think\facade\Db;
use think\View;

class Login{
    public function register(){
        $post = input('post.');
        if (!empty($post)){
            $pwd = $post['pwd'];
            $mobile = $post['mobile'];
            $username = $post['username'];
            $password = $post['password'];
            $corporate_name = $post['corporate_name'];
            $user = Db::name('user')
                ->where('username',$username)
                ->find();
            if (empty($username))
                $error = '账号必须填写';
            if (!empty($user['id']))
                $error = '账号已经存在';
            if (empty($password))
                $error = '密码必须填写';
            if ($password != $pwd)
                $error = '两次输入密码不一致';
            if (empty($mobile))
                $error = '联系方式必须填写';
            if (empty($corporate_name))
                $error = '企业名称必须填写';
            if (empty($error)){
                Db::name('user')
                    ->insert([
                        'username' => $username,
                        'password' => $password,
                        'mobile' => $mobile,
                        'corporate_name' =>  $corporate_name,
                        'pid' => 0,
                        'status' => 3,
                        'create_time' => date('Y-m-d H:i:s')
                      ]);
//                return View('/index/sucess');
                return View('/index/sccess',['msg' => '注册成功,等待管理员审核通过。','href' => '/index/index','']);
                die;
            }else {
                return View('/index/error', ['error' => $error, 'href' => '/index/register', 'msg' => '返回注册页面']);
            }
        }
        return View('/index/register');
    }

    public function index(){
        session_start();
        $post = input('post.');
        if(!empty($post)){
            $username = $post['username'];  
            $password = $post['password'];
            $user = Db::name('user')
                ->where('username',$username)

                ->find();
            if (empty($user['id']) || !is_null($user['delete_time'])){
                $error = '账号不存在';
            }else {
                $valid_username = $user['username'];
                $valid_password = $user['password'];
                // 检查用户名和密码是否正确
                if ($username === $valid_username && $password === $valid_password && $user['status'] == 1) {
                    // 登录成功，设置session变量
                    $_SESSION['user_loggedin'] = true;
                    $_SESSION['username'] = $username;
                    $_SESSION['timeout'] = time() + 86400;
                    // 重定向到受保护的页面或显示欢迎信息
                    Db::name('user')
                        ->where('id',$user['id'])
                        ->update(['login_time' => date('Y-m-d H:i:s')]);
                    header('Location:/index/index');
                    exit;
                }elseif($user['status'] == 2){
                    $error = '账号已经被禁用';
                } else {
                    // 登录失败，显示错误信息
                    $error = '用户名或密码错误';
                }
            }
            if (!empty($error))
                return View('/index/error',['error' => $error]);
        }
        return View('/index/login');
    }

    public function out(){
        session_start();
        $_SESSION = [];
        header('Location:/');
        die;
    }
}